/**
 * 交易模块 结构体数据转换
 */

package main

import (
    "gitee.com/mayiweb/gohs"
)

// 获得登录成功结构体数据
func GetLoginStruct(pRspUserLogin gohs.CHSRspUserLoginField) LoginStruct {

    defer CheckPanic()

    var sLogin LoginStruct

    sLogin.BranchID            = int(GetSwigInt32(pRspUserLogin.GetBranchID()))
    sLogin.AccountID           = pRspUserLogin.GetAccountID()
    sLogin.UserName            = pRspUserLogin.GetUserName()
    sLogin.TradingDay          = int(GetSwigInt32(pRspUserLogin.GetTradingDay()))
    sLogin.PreTradingDay       = int(GetSwigInt32(pRspUserLogin.GetPreTradingDay()))
    sLogin.BillConfirmFlag     = string(pRspUserLogin.GetBillConfirmFlag())
    sLogin.SessionID           = int(GetSwigInt32(pRspUserLogin.GetSessionID()))
    sLogin.UserApplicationType = string(pRspUserLogin.GetUserApplicationType())
    sLogin.UserApplicationInfo = pRspUserLogin.GetUserApplicationInfo()
    sLogin.RiskLevel           = pRspUserLogin.GetRiskLevel()
    sLogin.LastMacAddress      = pRspUserLogin.GetLastMacAddress()
    sLogin.LastIPAddress       = pRspUserLogin.GetLastIPAddress()
    sLogin.LastLoginTime       = int(GetSwigInt32(pRspUserLogin.GetLastLoginTime()))
    sLogin.CZCETime            = int(GetSwigInt32(pRspUserLogin.GetCZCETime()))
    sLogin.DCETime             = int(GetSwigInt32(pRspUserLogin.GetDCETime()))
    sLogin.SHFETime            = int(GetSwigInt32(pRspUserLogin.GetSHFETime()))
    sLogin.CFFEXTime           = int(GetSwigInt32(pRspUserLogin.GetCFFEXTime()))
    sLogin.INETime             = int(GetSwigInt32(pRspUserLogin.GetINETime()))
    sLogin.MaxOrderRef         = pRspUserLogin.GetMaxOrderRef()
    sLogin.UserID              = pRspUserLogin.GetUserID()
    sLogin.GFEXTime            = int(GetSwigInt32(pRspUserLogin.GetGFEXTime()))

    return sLogin
}

// 获得合约结构体数据
func GetInstrumentStruct(pInstrument gohs.CHSRspQryInstrumentField) InstrumentStruct {

    defer CheckPanic()

    var sInstrument InstrumentStruct

    sInstrument.ExchangeID           = pInstrument.GetExchangeID()
    sInstrument.ExchangeName         = GetExchangeName(pInstrument.GetExchangeID())
    sInstrument.InstrumentID         = pInstrument.GetInstrumentID()
    sInstrument.InstrumentName       = GbkToUtf8(pInstrument.GetInstrumentName())
    sInstrument.InstrumentEngName    = pInstrument.GetInstrumentEngName()
    sInstrument.ProductID            = pInstrument.GetProductID()
    sInstrument.ProductType          = string(pInstrument.GetProductType())
    sInstrument.ProductTypeTitle     = GetProductTypeTitle(string(pInstrument.GetProductType()))
    sInstrument.MaxMarketOrderVolume = int(pInstrument.GetMaxMarketOrderVolume())
    sInstrument.MinMarketOrderVolume = int(pInstrument.GetMinMarketOrderVolume())
    sInstrument.MaxLimitOrderVolume  = int(pInstrument.GetMaxLimitOrderVolume())
    sInstrument.MinLimitOrderVolume  = int(pInstrument.GetMinLimitOrderVolume())
    sInstrument.VolumeMultiple       = int(pInstrument.GetVolumeMultiple())
    sInstrument.PriceTick            = pInstrument.GetPriceTick()
    sInstrument.UnderlyingInstrID    = TrimSpace(pInstrument.GetUnderlyingInstrID())
    sInstrument.ExercisePrice        = pInstrument.GetExercisePrice()
    sInstrument.OptionsType          = string(pInstrument.GetOptionsType())
    sInstrument.TradingFlag          = string(pInstrument.GetTradingFlag())
    sInstrument.MarketDate           = GetSwigInt32(pInstrument.GetMarketDate())
    sInstrument.ExpireDate           = GetSwigInt32(pInstrument.GetExpireDate())
    sInstrument.BeginExerciseDate    = GetSwigInt32(pInstrument.GetBeginExerciseDate())
    sInstrument.EndExerciseDate      = GetSwigInt32(pInstrument.GetEndExerciseDate())
    sInstrument.BeginDeliveryDate    = GetSwigInt32(pInstrument.GetBeginDeliveryDate())
    sInstrument.EndDeliveryDate      = GetSwigInt32(pInstrument.GetEndDeliveryDate())
    sInstrument.CurrencyID           = string(pInstrument.GetCurrencyID())
    sInstrument.CombType             = string(pInstrument.GetCombType())
    sInstrument.MapKey               = pInstrument.GetInstrumentID()

    MapInstruments.Set(sInstrument.MapKey, sInstrument)

    return sInstrument
}

// 获得资金账户结构体数据
func GetAccountStruct(pTradingAccount gohs.CHSRspQryTradingAccountField) AccountStruct {

    defer CheckPanic()

    var sAccount AccountStruct

    sAccount.AccountID            = pTradingAccount.GetAccountID()
    sAccount.YdBalance            = Decimal(pTradingAccount.GetYdBalance(), 2)
    sAccount.YdPositionMargin     = Decimal(pTradingAccount.GetYdPositionMargin(), 2)
    sAccount.YdFundEquity         = Decimal(pTradingAccount.GetYdFundEquity(), 2)
    sAccount.FundEquity           = Decimal(pTradingAccount.GetFundEquity(), 2)
    sAccount.OptionsMarketValue   = Decimal(pTradingAccount.GetOptionsMarketValue(), 2)
    sAccount.Equity               = Decimal(pTradingAccount.GetEquity(), 2)
    sAccount.AvailableBalance     = Decimal(pTradingAccount.GetAvailableBalance(), 2)
    sAccount.WithdrawBalance      = Decimal(pTradingAccount.GetWithdrawBalance(), 2)
    sAccount.Margin               = Decimal(pTradingAccount.GetMargin(), 2)
    sAccount.FrozenMargin         = Decimal(pTradingAccount.GetFrozenMargin(), 2)
    sAccount.ExerciseFrozenMargin = Decimal(pTradingAccount.GetExerciseFrozenMargin(), 2)
    sAccount.RiskDegree           = Decimal(pTradingAccount.GetRiskDegree(), 2)
    sAccount.Premium              = Decimal(pTradingAccount.GetPremium(), 2)
    sAccount.FrozenPremium        = Decimal(pTradingAccount.GetFrozenPremium(), 2)
    sAccount.Commission           = Decimal(pTradingAccount.GetCommission(), 2)
    sAccount.FrozenCommission     = Decimal(pTradingAccount.GetFrozenCommission(), 2)
    sAccount.CloseProfit          = Decimal(pTradingAccount.GetCloseProfit(), 2)
    sAccount.PositionProfit       = Decimal(pTradingAccount.GetPositionProfit(), 2)
    sAccount.CloseProfitByDate    = Decimal(pTradingAccount.GetCloseProfitByDate(), 2)
    sAccount.PositionProfitByDate = Decimal(pTradingAccount.GetPositionProfitByDate(), 2)
    sAccount.Deposit              = Decimal(pTradingAccount.GetDeposit(), 2)
    sAccount.Withdraw             = Decimal(pTradingAccount.GetWithdraw(), 2)
    sAccount.FundMortgage         = Decimal(pTradingAccount.GetFundMortgage(), 2)
    sAccount.WarrantMortgage      = Decimal(pTradingAccount.GetWarrantMortgage(), 2)
    sAccount.FrozenBalance        = Decimal(pTradingAccount.GetFrozenBalance(), 2)
    sAccount.UnFrozenBalance      = Decimal(pTradingAccount.GetUnFrozenBalance(), 2)
    sAccount.CurrencyID           = string(pTradingAccount.GetCurrencyID())
    sAccount.HedgeRiskDegree      = Decimal(pTradingAccount.GetHedgeRiskDegree(), 2)
    sAccount.MapKey               = sAccount.AccountID

    MapAccount.Set(sAccount.MapKey, sAccount)

    return sAccount
}

// 获得报单结构体数据
func GetOrderStruct(pOrder gohs.CHSOrderField) OrderStruct {

    defer CheckPanic()

    var sOrder OrderStruct

    sOrder.InstrumentID     = pOrder.GetInstrumentID()
    sOrder.OrderStatus      = string(pOrder.GetOrderStatus())
    sOrder.OrderStatusTitle = GetOrderStatusTitle(string(pOrder.GetOrderStatus()))
    sOrder.Direction        = string(pOrder.GetDirection())
    sOrder.DirectionTitle   = GetDirectionTitle(string(pOrder.GetDirection()))
    sOrder.OffsetFlag       = string(pOrder.GetOffsetFlag())
    sOrder.OffsetFlagTitle  = GetOffsetFlagTitle(string(pOrder.GetOffsetFlag()))
    sOrder.OrderSysID       = pOrder.GetOrderSysID()
    sOrder.ExchangeID       = pOrder.GetExchangeID()
    sOrder.SessionID        = GetSwigInt32(pOrder.GetSessionID())
    sOrder.OrderRef         = pOrder.GetOrderRef()
    sOrder.OrderPrice       = Decimal(pOrder.GetOrderPrice(), 4)
    sOrder.OrderVolume      = int(pOrder.GetOrderVolume())
    sOrder.InsertTime       = HsTime(Int32ToString(GetSwigInt32(pOrder.GetInsertTime())))
    sOrder.InstrumentName   = GetInstrumentName(pOrder.GetInstrumentID())
    sOrder.ErrorMsg         = string(pOrder.GetErrorMsg())
    sOrder.MapKey           = sOrder.OrderSysID

    // 只记录有报单编号的报单数据
    if sOrder.OrderSysID != "" {
        MapOrders.Set(sOrder.MapKey, sOrder)
    }

    return sOrder
}

// 获得持仓结构体数据
func GetPositionStruct(pPosition gohs.CHSRspQryPositionField) PositionStruct {

    defer CheckPanic()

    var sPosition PositionStruct

    Instrument, _ := GetInstrumentInfo(pPosition.GetInstrumentID())

    sPosition.ExchangeID              = pPosition.GetExchangeID()
    sPosition.InstrumentID            = pPosition.GetInstrumentID()
    sPosition.InstrumentName          = Instrument.InstrumentName
    sPosition.Direction               = string(pPosition.GetDirection())
    sPosition.DirectionTitle          = GetDirectionTitle(string(pPosition.GetDirection()))
    sPosition.HedgeType               = string(pPosition.GetHedgeType())
    sPosition.HedgeTypeTitle          = GetHedgeTypeTitle(string(pPosition.GetHedgeType()))
    sPosition.PositionVolume          = int(pPosition.GetPositionVolume())
    sPosition.OpenCost                = Decimal(pPosition.GetOpenCost(), 4)
    sPosition.PositionCost            = Decimal(pPosition.GetPositionCost(), 4)
    sPosition.PositionProfit          = Decimal(pPosition.GetPositionProfit(), 2)
    sPosition.PositionPrice           = Decimal(sPosition.PositionCost / float64(sPosition.PositionVolume) / float64(Instrument.VolumeMultiple), 4)
    sPosition.UnderlyingInstrID       = Instrument.UnderlyingInstrID
    sPosition.CloseFrozenVolume       = int(pPosition.GetCloseFrozenVolume())
    sPosition.AvailablePositionVolume = int(pPosition.GetAvailablePositionVolume())
    sPosition.MapKey                  = sPosition.InstrumentID + "_" + sPosition.Direction

    MapPositions.Set(sPosition.MapKey, sPosition)

    return sPosition
}


// 获得报单成交结构体数据
func GetTradeStruct(pTrade gohs.CHSTradeField) TradeStruct {

    defer CheckPanic()

    var sTrade TradeStruct

    sTrade.AccountID         = pTrade.GetAccountID()
    sTrade.TradeID           = pTrade.GetTradeID()
    sTrade.OrderSysID        = pTrade.GetOrderSysID()
    sTrade.BrokerOrderID     = pTrade.GetBrokerOrderID()
    sTrade.SessionID         = int(GetSwigInt32(pTrade.GetSessionID()))
    sTrade.OrderRef          = pTrade.GetOrderRef()
    sTrade.ExchangeID        = pTrade.GetExchangeID()
    sTrade.InstrumentID      = pTrade.GetInstrumentID()
    sTrade.InstrumentName    = GetInstrumentName(pTrade.GetInstrumentID())
    sTrade.Direction         = string(pTrade.GetDirection())
    sTrade.DirectionTitle    = GetDirectionTitle(string(pTrade.GetDirection()))
    sTrade.OffsetFlag        = string(pTrade.GetOffsetFlag())
    sTrade.OffsetFlagTitle   = GetOffsetFlagTitle(string(pTrade.GetOffsetFlag()))
    sTrade.HedgeType         = string(pTrade.GetHedgeType())
    sTrade.TradeVolume       = int(pTrade.GetTradeVolume())
    sTrade.TradePrice        = Decimal(pTrade.GetTradePrice(), 4)
    sTrade.TradingDay        = int(GetSwigInt32(pTrade.GetTradingDay()))
    sTrade.TradeTime         = HsTime(Int32ToString(GetSwigInt32(pTrade.GetTradeTime())))
    sTrade.UnderlyingInstrID = pTrade.GetUnderlyingInstrID()
    sTrade.CombPositionID    = pTrade.GetCombPositionID()
    sTrade.TradeCommission   = pTrade.GetTradeCommission()
    sTrade.MapKey            = Sprintf("%v_%v", StringToInt(sTrade.TradeID), sTrade.InstrumentID)

    // 将报单成交数据记录下来
    MapTrades.Set(sTrade.MapKey, sTrade)

    return sTrade
}